package com.xinggq.user.service.dto.user;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.xinggq.user.service.entity.SysUserEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 用户信息VO
 *
 * @author xinggq
 * @since 2025-01-01
 */
@Data
@Schema(description = "用户信息")
public class UserVO {

    @Schema(description = "用户ID", example = "1")
    private Long id;

    @Schema(description = "租户ID", example = "1")
    private Long tenantId;

    @Schema(description = "用户名", example = "admin")
    private String username;

    @Schema(description = "邮箱", example = "admin@example.com")
    private String email;

    @Schema(description = "手机号", example = "13800138000")
    private String phone;

    @Schema(description = "真实姓名", example = "张三")
    private String realName;

    @Schema(description = "昵称", example = "管理员")
    private String nickname;

    @Schema(description = "性别：0-未知，1-男，2-女", example = "1")
    private Integer gender;

    @Schema(description = "生日", example = "1990-01-01")
    @JsonFormat(pattern = "yyyy-MM-dd" , timezone = "GMT+8")
    private LocalDate birthday;

    @Schema(description = "用户状态：1-正常，0-禁用", example = "1")
    private Integer status;

    @Schema(description = "状态描述", example = "正常")
    private String statusDesc;

    @Schema(description = "是否管理员：0-否，1-是", example = "0")
    private Integer isAdmin;

    @Schema(description = "最后登录时间", example = "2025-01-01 12:00:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime lastLoginTime;

    @Schema(description = "最后登录IP", example = "192.168.1.1")
    private String lastLoginIp;

    @Schema(description = "登录次数", example = "10")
    private Integer loginCount;

    @Schema(description = "创建时间", example = "2025-01-01 12:00:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createdAt;

    @Schema(description = "更新时间", example = "2025-01-01 12:00:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime updatedAt;

    @Schema(description = "创建人ID", example = "1")
    private Long createdBy;

    @Schema(description = "更新人ID", example = "1")
    private Long updatedBy;

    /**
     * 从实体转换为VO
     */
    public static UserVO fromEntity(SysUserEntity entity) {
        if (entity == null) {
            return null;
        }
        
        UserVO vo = new UserVO();
        vo.setId(entity.getId());
        vo.setTenantId(entity.getTenantId());
        vo.setUsername(entity.getUsername());
        vo.setEmail(entity.getEmail());
        vo.setPhone(entity.getPhone());
        vo.setRealName(entity.getRealName());
        vo.setNickname(entity.getNickname());
        vo.setGender(entity.getGender());
        vo.setBirthday(entity.getBirthday());
        vo.setStatus(entity.getStatus());
        vo.setStatusDesc(entity.getStatus() != null && entity.getStatus() == 1 ? "正常" : "禁用");
        vo.setLastLoginTime(entity.getLastLoginTime());
        vo.setLastLoginIp(entity.getLastLoginIp());
        vo.setLoginCount(entity.getLoginCount());
        vo.setCreatedAt(entity.getCreatedAt());
        vo.setUpdatedAt(entity.getUpdatedAt());
        vo.setCreatedBy(entity.getCreatedBy());
        vo.setUpdatedBy(entity.getUpdatedBy());
        
        return vo;
    }
}